package UI;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.BorderFactory;
import javax.swing.GroupLayout;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.JTableHeader;

import DBTasks.Adoption;
import DBTasks.DetailedAdoptionsForShelter;

@SuppressWarnings("serial")
public class DailyPanel extends JPanel implements ActionListener{

	private JButton returnButton;
	private DefaultTableModel model;
	private JTable table;
	private JTableHeader headers;
	private JTextField dateInput;
	private MainFrame frame;
	private String shelterName;
	String[] columnNames = {"Name", "Date of Birth", "Phone", "Address", "Pet ID", "Type"};

	public DailyPanel(MainFrame mainFrame) {
		
		//this.setSize(850, 600);
		this.frame = mainFrame;
		
		JLabel logo = new JLabel("Atlanta Pet Adoption Consortium");
		logo.setFont(new Font("Tahoma", Font.BOLD, 30));
		
		shelterName = (String) frame.getLogin().getShelterList().getSelectedItem();
		JLabel shelterLabel = new JLabel("Adoptions for " + shelterName);
		shelterLabel.setFont(new Font("Tahoma", Font.PLAIN, 18));
		
		JLabel dateLabel = new JLabel("Enter Date: ");
		dateLabel.setFont(new Font("Tahoma", Font.PLAIN, 18));
		dateInput = new JTextField(20);
	    dateInput.setToolTipText("Press Enter when you have put in a correct date");
		dateInput.addActionListener(this);
		
		returnButton = new JButton("Return");
		returnButton.addActionListener(this);
		
		Object[][] data = {};
		model = new DefaultTableModel(data, columnNames);
		table = new JTable(model);
		table.setGridColor(Color.black);
		headers = table.getTableHeader();
		table.setBorder(BorderFactory.createLineBorder(Color.black));
		headers.setBorder(BorderFactory.createLineBorder(Color.black));
		table.setRowHeight(30);
		
        GroupLayout gl = new GroupLayout(this);
		
		gl.setHorizontalGroup(gl.createParallelGroup(GroupLayout.Alignment.CENTER)
				.addComponent(logo)
				.addGroup(gl.createSequentialGroup()
						.addComponent(shelterLabel)
						.addGap(425)
						.addComponent(dateLabel)
						.addComponent(dateInput))
				.addComponent(headers)
				.addComponent(table)
				.addGroup(gl.createSequentialGroup()
						.addGap(850)
						.addComponent(returnButton)));
		
		gl.linkSize(SwingConstants.VERTICAL, dateInput);
		
	    gl.setVerticalGroup(gl.createSequentialGroup()
	    		.addGap(15)
			    .addComponent(logo)
			    .addGap(40)
			    .addGroup(gl.createParallelGroup(GroupLayout.Alignment.CENTER)
						.addComponent(shelterLabel)
						.addComponent(dateLabel)
						.addComponent(dateInput))
			    .addGap(20)
			    .addComponent(headers)
			    .addComponent(table)
			    .addGap(20)
			    .addComponent(returnButton)
			    .addGap(40)
	    );
		
		gl.setAutoCreateContainerGaps(true);
		
		this.setLayout(gl);
		
	}
	
	public void paintComponent(Graphics g) {
        super.paintComponents(g);
        g.setColor(Color.blue);
        g.fillRect(0, 0, this.getWidth(), 70);
    }

	@Override
	public void actionPerformed(ActionEvent e) {
		if(e.getSource().equals(returnButton)) {
			frame.setPanel(frame.getHomepage());
		}
		else if(e.getSource().equals(dateInput)) {
			createTable();
			table.getColumnModel().getColumn(1).setPreferredWidth(75);
			table.getColumnModel().getColumn(3).setPreferredWidth(150);
			frame.setPanel(frame.getDaily());
		}
	}
	
	public void createTable() {
		ArrayList<Adoption> adoptions = DetailedAdoptionsForShelter.getDetailedAdoptions(dateInput.getText(), shelterName);
		Object[][] data = new Object[adoptions.size()][6];
		for(int i = 0; i < data.length; i++) {
			data[i][0] = adoptions.get(i).getName();
			data[i][1] = adoptions.get(i).getDateOfBirth();
			data[i][2] = adoptions.get(i).getPhone();
			data[i][3] = adoptions.get(i).getAddress();
			data[i][4] = adoptions.get(i).getPetId();
			data[i][5] = adoptions.get(i).getType();
		}
		model = new DefaultTableModel(data, columnNames);
		table.setModel(model);
	}

}
